from globals import *
from pandac.PandaModules import *
from panda3d.core import PointLight,Spotlight


class racer(DirectObject):
    def __init__(self, car, displayZone, menuCars):
        #Create an actor for a particular car
        self.car = car
        path = "models/" + self.car
        self.racer = Actor(path + "/car",
                        {"accelerate":path+"/accelerate",
                         "turn_left":path+"/turn_left",
                         "turn_right":path+"/turn_right",
                         "brake":path+"/car",
                         "fire_long":path+"/long_range",
                         "fire_short":path+"/close_range",
                         "death":path+"/death"
                         })
        if(menuCars): #set up basic camera
            self.camera = self.spawnCamera( (0,0,0,0) )
            self.camera.setPos(0,0,0)
        else:#use displayZone to create a camera and view.
            self.camera = self.spawnCamera( displayZone )
            self.camera.reparentTo(self.racer)
            self.camera.lookAt(self.racer)
            base.camNode.setActive(False)
            taskMgr.add(self.steadyCamera, "steadyCamera")
    
        # h, w, l
        # 12,12,29
        
        self.cTrav = CollisionTraverser()
        
        self.racer.enableBlend()
        self.racer.setScale(.0025)
        self.racer.reparentTo(render)
     
        #NOT WORKING
        #self.headlightLeft = Spotlight ('headlightLeft')
        #self.headlightLeft.setColor(VBase4(1,1,1,1))
        #self.lens1 = PerspectiveLens()
        #self.headlightLeft.setLens(self.lens1)
        #self.hdnp = self.racer.attachNewNode(self.headlightLeft.upcastToLensNode())
        #render.setLight(self.headlightLeft)
        # self.lightAttr = LightAttrib.makeAllOff()
        # 
        #         self.headlightLeft = Spotlight( "headlightLeft" )
        #         self.headlightLeft.setColor( Vec4( 1, 1, 1, 1 ) )
        #         self.headlightLeft.setLens( PerspectiveLens() )
        #         self.headlightLeft.getLens().setFov( 25, 25 )
        #         self.headlightLeft.setAttenuation( Vec3( 1.0, 0.0, 0.0 ) )
        #         self.headlightLeft.setExponent( 1.0 )
        #         self.spotnode1 = self.racer.attachNewNode(self.headlightLeft.upcastToLensNode())
        #         self.spotnode1.setHpr(180,0,0)
        #         self.lightAttr = self.lightAttr.addLight( self.headlightLeft )
        #         
        #         self.headlightRight = Spotlight( "headlightRight" )
        #         self.headlightRight.setColor( Vec4( 1, 1, 1, 1 ) )
        #         self.headlightRight.setLens( PerspectiveLens() )
        #         self.headlightRight.getLens().setFov( 25, 25 )
        #         self.headlightRight.setAttenuation( Vec3( 1.0, 0.0, 0.0 ) )
        #         self.headlightRight.setExponent( 1.0 )
        #         self.spotnode2 = self.racer.attachNewNode(self.headlightRight.upcastToLensNode())
        #         self.spotnode2.setHpr(180,0,0)
        #         self.lightAttr = self.lightAttr.addLight( self.headlightRight )
        #         render.node().setAttrib(self.lightAttr)
        
        #insert starting position here.
        if(self.car == "pirate"):
            self.racer.setPos(-.12,0,0)
            self.camera.setPos(self.racer.getX(),self.racer.getY()+10,125)
        if(self.car == "knight"):
            self.racer.setPos(-.04,0,0)
            self.camera.setPos(self.racer.getX(),self.racer.getY()+10,125)
        if(self.car == "viking"):
            self.racer.setPos(.04,0,0)
            self.camera.setPos(self.racer.getX(),self.racer.getY()+10,125)
        if(self.car == "caveman"):
            self.racer.setPos(.12,0,0)
            self.camera.setPos(self.racer.getX(),self.racer.getY()+10,125)

        self.velocity = 1.0
        self.ammo = 0
        self.health = 100
        
#        self.racerGroundRay = CollisionRay()
#        self.racerGroundRay.setOrigin(0,0,1000)
#        self.racerGroundRay.setDirection(0,0,-1)
#        self.racerGroundCol = CollisionNode('racerRay')
#        self.racerGroundCol.addSolid(self.racerGroundRay)
#        self.racerGroundCol.setFromCollideMask(BitMask32.bit(0))
#        self.racerGroundCol.setIntoCollideMask(BitMask32.allOff())
#        self.racerGroundColNp = self.racer.attachNewNode(self.racerGroundCol)
#        self.racerGroundHandler = CollisionHandlerQueue()
#        self.cTrav.addCollider(self.racerGroundColNp, self.racerGroundHandler)

#        self.camGroundRay = CollisionRay()
#        self.camGroundRay.setOrigin(0,0,1000)
#        self.camGroundRay.setDirection(0,0,-1)
#        self.camGroundCol = CollisionNode('camRay')
#        self.camGroundCol.addSolid(self.camGroundRay)
#        self.camGroundCol.setFromCollideMask(BitMask32.bit(0))
#        self.camGroundCol.setIntoCollideMask(BitMask32.allOff())
#        self.camGroundColNp = self.camera.attachNewNode(self.camGroundCol)
#        self.camGroundHandler = CollisionHandlerQueue()
#        self.cTrav.addCollider(self.camGroundColNp, self.camGroundHandler)
        
        
    
    
    def spawnCamera(self,dispRegion):
        camera = base.makeCamera(base.win,displayRegion = dispRegion)
        camera.node().getLens().setAspectRatio(3.0/4.0)
        camera.node().getLens().setFov(45)
        if(self.car == "pirate"):
            camera.setPos(-.12,-.1,2)
        if(self.car == "knight"):
            camera.setPos(-.04,-.1,2)
        if(self.car == "viking"):
            camera.setPos(.04,-.1,2)
        if(self.car == "caveman"):
            camera.setPos(.12,-.1,2)
        return camera
    
    def steadyCamera(self, task):
        camvec = self.racer.getPos() - self.camera.getPos()
        camvec.setZ(0)
        camdist = camvec.length()
        camvec.normalize()
        
        if(camdist > 10.0):
            self.camera.setPos(self.camera.getPos() + camvec*(camdist-10))
            camdist = 10.0
        if(camdist < 5.0):
            self.camera.setPos(self.camera.getPos() - camvec*(5-camdist))
            camdist = 5.0
            
            
# Now we create a spotlight. Spotlights light objects in a given cone
    # They are good for simulating things like flashlights
    # self.spotlight = racer.attachNewNode( Spotlight( "spotlight" ) )
	# self.spotlight.node().setHpr(0,0,0)
    # self.spotlight.node().setColor( Vec4( .45, .45, .45, 1 ) )
    # The cone of a spotlight is controlled by it's lens. This creates the lens
    # self.spotlight.node().setLens( PerspectiveLens() )
    # This sets the Field of View (fov) of the lens, in degrees for width and
    # height. The lower the numbers, the tighter the spotlight.
    # self.spotlight.node().getLens().setFov( 5, 5 )
    # Attenuation controls how the light fades with distance. The numbers are
    # The three values represent the three constants (constant, linear, and
    # quadratic) in the internal lighting equation. The higher the numbers the
    # shorter the light goes.
    # self.spotlight.node().setAttenuation( Vec3( 1, 0.0, 0.0 ) ) 
    # This exponent value sets how soft the edge of the spotlight is. 0 means a
    # hard edge. 128 means a very soft edge.
    # self.spotlight.node().setExponent( 100.0 )
    
    #Spotlights represent the most sophisticated kind of light. A spotlight has both a point and a direction, and a field-of-view. In fact, a spotlight contains a lens, just like a camera does; 
    #the lens should be a PerspectiveLens and is used to define the area of effect of the light (the light illuminates everything within the field of view of the lens).
    #Note that the English word "spotlight" is one word, as opposed to the other kinds of lights, which are two words. Thus, the class name is correctly spelled "Spotlight", not "SpotLight".
    #Also, because a spotlight has a different inheritance than the other kinds of lights, you need to use upcastToLensNode() instead of upcastToPandaNode().
    #
    #slight = Spotlight('slight') 
    #slight.setColor(VBase4(1, 1, 1, 1)) 
    #lens = PerspectiveLens() 
    #slight.setLens(lens) 
    #slnp = render.attachNewNode(slight.upcastToLensNode()) 
    #slnp.setPos(10, 20, 0) 
    #slnp.lookAt(myObject) 
    #render.setLight(slnp)